1082A - Vasya and Book - CodeForces Solution


implementation math *1200

Please click on ads to support us..

Python Code:

for s in [*open(0)][1:]:
  n,x,y,d=map(int,s.split())
  a,b,c=abs(x-y)//d,(x+d-1)//d+(y-1)//d,-((x-n)//d)+(n-y)//d
  r=a if x%d==y%d else -1
  if r<0 and y%d==1:r=b
  if n%d==y%d:r=[min(r,c),c][r<0]
  print(r)

C++ Code:

#pragma GCC optimize(0)
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)

#include <iostream>
#include <map>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <cstring>

#define fi  first
#define se  second
#define endl "\n"
#define int long long
#define loop(i,a,b) for(int i = a;i <= b;i ++)
#define pool(i,a,b) for(int i = a;i >= b;i --)
using namespace std;
typedef pair<int,int> PII;
inline void YES(){cout << "YES" << endl;}
inline void NO(){cout << "NO" << endl;}

const int N = 2e5 + 5,M = 3e3 + 5,S = 1e6 + 7,Mod = 998244353;

void solve(){
    int n,x,y,d;
    cin >> n >> x >> y >> d;
    
    int ans = 1e18;
    int len = abs(x - y);
    
    //直接走
    if(len % d == 0) ans = min(ans,len / d);    
    //先走到1;
    if((y - 1) % d == 0) {
        ans = min(ans,(x + d - 2) / d + (y - 1) / d);
    }
    
    if((n - y) % d == 0) {
        ans = min(ans,(n - x + d - 1) / d + (n - y) / d);
    }
    
    if(ans == 1e18) ans = -1;
    cout << ans << endl;
}

signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int t = 1;
    cin >> t;
    
    while(t --){
        solve();
    }
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles